package com.baijiaoxi.common.model.mq;

import com.baijiaoxi.common.model.KeyValueManager;
import com.baijiaoxi.common.model.UUID;
import com.baijiaoxi.common.model.local.ThreadLocalUtil;
import com.baijiaoxi.common.model.request.RequestInfo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

@Data
@Slf4j
public class MQBody implements Serializable {
    protected String messageId;
    protected Long weappId;
    protected Long accountId;
    protected Long userId;
    protected RequestInfo requestInfo;

    public boolean notUsed(String clientId, KeyValueManager keyValueManager) {
        String key = clientId + "-" + this.getMessageId();
        boolean hasUsed = keyValueManager.hasKeySilent(key);
        if (!hasUsed)
            keyValueManager.setSilent(key, "", 3600 * 6);
        return !hasUsed;
    }

    public MQBody(String messageId) {
        this.messageId = messageId;
        try {
            this.requestInfo = ThreadLocalUtil.get();
            this.accountId = requestInfo.getAccountId();
            this.weappId = requestInfo.getWeappId();
            this.userId = requestInfo.getUserId();
        } catch (Exception e) {
            //  log.error("无法获取requestInfo");
        }
    }

    public MQBody() {
        try {
            this.messageId = UUID.uuid();
            this.requestInfo = ThreadLocalUtil.get();
            this.accountId = requestInfo.getAccountId();
            this.weappId = requestInfo.getWeappId();
            this.userId = requestInfo.getUserId();
        } catch (Exception e) {
            //  log.error("无法获取requestInfo");
        }
    }
}
